package com.minelx.scriptbuilder.parse.matcher;

import com.minelx.scriptbuilder.script.attribute.function.Params;
import com.minelx.sequence.ReturnMatcher;
import com.minelx.scriptbuilder.parse.parser.ParamsParser;
import com.minelx.scriptbuilder.parse.parser.CommonValueMatcher.Affection;
import com.minelx.scriptbuilder.script.IVariable;
import com.minelx.scriptbuilder.script.ScriptContext;
import com.minelx.scriptbuilder.script.attribute.value.IAttribute;

public class ExecuteMatcher implements ReturnMatcher<Affection, Affection> {

	private final ScriptContext context;

	public ExecuteMatcher(ScriptContext context) {
		this.context = context;
	}

	@Override
	public boolean isMatch(Affection bundle) {
		return bundle.getScanner().exists("(");
	}

	@Override
	public Affection onMatched(Affection affection) {
		affection.getScanner().nextChar(); // '('
		Params params = new ParamsParser(context).parse(affection.getScanner());
		affection.getScanner().nextChar(); // ')'

		IVariable belongs = affection.belongs();
		affection.effectedBy(IVariable.immediate(() -> belongs.value().execute(params)));
		return affection;
	}
}
